﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Monster : MonoBehaviour
{
    private Rigidbody2D rb;
    private Collider2D coll;
    [Header("参考参数")]
    private bool faceleft = true;
    public Transform tsfl, tsfr;
    [Header("移动参数")]
    public float speed;
   
    void Start()
    {
        transform.DetachChildren();
        rb = GetComponent<Rigidbody2D>();
        coll = GetComponent<Collider2D>();
    }

    void Update()
    {
        move();
    }


    void move()
    {
        if (faceleft == true) 
        {
            rb.velocity = new Vector2(-speed, rb.velocity.y);
            if (transform.position.x < tsfl.position.x)
            { 
                transform.localScale = new Vector3(-3, transform.localScale.y, transform.localScale.z);
                faceleft = false;
            }

        }
        else
        {
            rb.velocity = new Vector2(speed, rb.velocity.y);
            if (transform.position.x > tsfr.position.x)
            {
                transform.localScale = new Vector3(3, transform.localScale.y, transform.localScale.z);
                faceleft = true;
            }
        }

    }
}
